package cn.iocoder.yudao.module.cmt.controller.admin.base;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.cmt.controller.admin.base.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.base.BaseMerchantApplyDO;
import cn.iocoder.yudao.module.cmt.service.base.BaseMerchantApplyService;

@Tag(name = "管理后台 - 商户入驻申请")
@RestController
@RequestMapping("/cmt/base-merchant-apply")
@Validated
public class BaseMerchantApplyController {

    @Resource
    private BaseMerchantApplyService merchantApplyService;

    @PostMapping("/create")
    @Operation(summary = "创建商户入驻申请")
    @PreAuthorize("@ss.hasPermission('cmt:base-merchant-apply:create')")
    public CommonResult<Long> createMerchantApply(@Valid @RequestBody BaseMerchantApplySaveReqVO createReqVO) {
        return success(merchantApplyService.createMerchantApply(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商户入驻申请")
    @PreAuthorize("@ss.hasPermission('cmt:base-merchant-apply:update')")
    public CommonResult<Boolean> updateMerchantApply(@Valid @RequestBody BaseMerchantApplySaveReqVO updateReqVO) {
        merchantApplyService.updateMerchantApply(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商户入驻申请")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cmt:base-merchant-apply:delete')")
    public CommonResult<Boolean> deleteMerchantApply(@RequestParam("id") Long id) {
        merchantApplyService.deleteMerchantApply(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除商户入驻申请")
    @PreAuthorize("@ss.hasPermission('cmt:base-merchant-apply:delete')")
    public CommonResult<Boolean> deleteMerchantApplyList(@RequestParam("ids") List<Long> ids) {
        merchantApplyService.deleteMerchantApplyListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商户入驻申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cmt:base-merchant-apply:query')")
    public CommonResult<BaseMerchantApplyRespVO> getMerchantApply(@RequestParam("id") Long id) {
        BaseMerchantApplyDO merchantApply = merchantApplyService.getMerchantApply(id);
        return success(BeanUtils.toBean(merchantApply, BaseMerchantApplyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商户入驻申请分页")
    @PreAuthorize("@ss.hasPermission('cmt:base-merchant-apply:query')")
    public CommonResult<PageResult<BaseMerchantApplyRespVO>> getMerchantApplyPage(@Valid BaseMerchantApplyPageReqVO pageReqVO) {
        PageResult<BaseMerchantApplyDO> pageResult = merchantApplyService.getMerchantApplyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BaseMerchantApplyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商户入驻申请 Excel")
    @PreAuthorize("@ss.hasPermission('cmt:base-merchant-apply:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMerchantApplyExcel(@Valid BaseMerchantApplyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BaseMerchantApplyDO> list = merchantApplyService.getMerchantApplyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商户入驻申请.xls", "数据", BaseMerchantApplyRespVO.class,
                        BeanUtils.toBean(list, BaseMerchantApplyRespVO.class));
    }

}
